package com.jiashihui.server1.controller;

import com.jiashihui.encrypt.anno.Decrypt;
import com.jiashihui.encrypt.anno.Encrypt;
import com.jiashihui.server1.annotation.RateLimitAspect;
import com.jiashihui.server1.model.User;
import com.jiashihui.server1.model.base.BaseResponse;
import com.jiashihui.server1.model.validated.Insert;
import com.jiashihui.server1.model.validated.Sequence;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
  * 
  * @ProjectName:    
  * @Package:        com.example.server1.controller
  * @ClassName:      TempController
  * @Description:    java类作用描述
  * @Author:         jiashihui
  * @CreateDate:     2018/11/6 15:36
  * @UpdateUser:     更新者
  * @UpdateDate:     2018/11/6 15:36
  * @UpdateRemark:   更新说明
  * @Version:        1.0
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TempController {

    /**
     * @author: jiashihui
     * @description 描述一下方法的作用
     * @date: 2018/11/30 16:15
     * @param user
     * @param br
     * @return
     */
    @Encrypt
    @Decrypt
    @PostMapping(value="/encrypt", consumes = "application/json", produces = "application/json; charset=utf-8")
    public BaseResponse test(@RequestBody @Validated User user, BindingResult br){
        BaseResponse response = new BaseResponse("-200", "请求失败！");
        return response;
    }

    /**
     * @method  testLog
     * @description 日志测试
     * @date: 2018/12/18 10:14
     * @author: jiashihui
     * @return String
     */
    //@CrossOrigin
    //@CrossOrigin(origins = "http://domain2.com",maxAge = 3600,methods = {RequestMethod.GET, RequestMethod.POST}
    @GetMapping(value="/log", consumes = "application/json; charset=utf-8", produces = "application/json; charset=utf-8")
    public BaseResponse testLog(){
        log.debug("aaaaaaa{}","yyy");
        log.info("333333333{}", "yyyy");
        BaseResponse response = new BaseResponse("-200", "请求失败！");
        return response;
    }

    /**
     * @method  testValidation
     * @description 描述一下方法的作用
     * @date: 2018/11/28 17:21
     * @author: jiashihui
     * @return 
     */
    @Encrypt
    @GetMapping(value="/validation", produces = "application/json; charset=utf-8")
    public String testValidation(@Validated({Insert.class}) User user, BindingResult br){
//        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
//        Validator validator = factory.getValidator();

        //Car car = new Car("汽车1", "X",1);
        //Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );
//        if(constraintViolations.size() != 0){
//            String errMessage = "";
//            for(ConstraintViolation<Car> c : constraintViolations) {
//                errMessage += c.getMessage() + ";";
//            }
//            log.info(errMessage);
//            return errMessage;
//        }

        if(br.hasErrors()){
            StringBuilder errMessage = new StringBuilder();
            for(FieldError c : br.getFieldErrors()) {
                errMessage.append(c.getDefaultMessage() + ";");
            }
            log.info(errMessage.toString());
            return errMessage.toString();
        }
        return user.getSalary() + "";
    }

    /**
     * @method  testValidation
     * @description 描述一下方法的作用
     * @date: 2018/11/28 17:21
     * @author: jiashihui
     * @return
     */
    @RateLimitAspect//并发控制
    @GetMapping(value="/validation2", produces = "application/json; charset=utf-8")
    public String testValidation2(@Validated({Sequence.class}) User user, BindingResult br){
        if(br.hasErrors()){
            StringBuilder errMessage = new StringBuilder();
            for(FieldError c : br.getFieldErrors()) {
                errMessage.append(c.getField() + ":" + c.getDefaultMessage() + ";");
            }
            log.info(errMessage.toString());
            return errMessage.toString();
        }
        return user.getSalary() + "";
    }

}
